#< { 'use-with':'Pacify' , 'type':'input' , 'name':'Office Open XML' , 'argvalue':'docx' , 'extension':'docx'  , 'version':'2011.1003' , 'author':'AGGOTT HÖNSCH István'} >#

from ptext import *

import zipfile
from xml.parsers.expat import *

def load_module(self):
	self.addMethod(init_input)
	self.addMethod(readfile)
	self.addMethod(xmlCharData)
	self.addMethod(xmlStartElementHandler)
	self.addMethod(xmlEndElementHandler)

def init_input(self):
	
	return True

def readfile(self):
	
	self.log.info("Loading "+self.infilename+" into memory...")
	
	zip = zipfile.ZipFile(self.infilename)
	
	original = zip.read('word/document.xml')
	
	self.buffer = RichString()
	
	self.xmldoc = ParserCreate()
	self.xmldoc.CharacterDataHandler = self.xmlCharData
	self.xmldoc.StartElementHandler = self.xmlStartElementHandler
	self.xmldoc.EndElementHandler = self.xmlEndElementHandler
	self.xmldoc.Parse(original)
	
	original = ''
	
	self.log.info("Successfully loaded "+self.infilename+".")
	
	return True

def xmlCharData(self, data):
	self.buffer.Append(str(data))
	return

def xmlStartElementHandler(self, name, attrs):
	
	if name == "w:p":
		self.buffer.itsFocusRole = ''
		self.buffer.Append("\n\n")
	elif name == "w:pStyle":
		if attrs['w:val'] == 'Heading1':
			self.buffer.itsFocusRole = 'chapter'
	
	return

def xmlEndElementHandler(self, name):
	return

#<END>#

if __name__ == "__main__":
	print("\n\tThis is a Pacify plugin and cannot be executed directly.")